package com.caiju.health.service.impl;

import com.caiju.health.dto.MonthsAndMemberCountDTO;
import com.caiju.health.dto.WorkCountDTO;
import com.caiju.health.entity.PageResult;
import com.caiju.health.mapper.ICheckItemMapper;
import com.caiju.health.mapper.IMemberMapper;
import com.caiju.health.pojo.CheckItem;
import com.caiju.health.pojo.File;
import com.caiju.health.pojo.Member;
import com.caiju.health.service.ICheackItemService;
import com.caiju.health.service.IMemberService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MemberServiceImpl implements IMemberService {
    @Resource
    private IMemberMapper memberMapper;

    @Override
    public MonthsAndMemberCountDTO getMemberReport() throws ParseException {
        //统计的数据时动态的月份
        //获取当前的月份信息
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR)-1    ;
        int month = calendar.get(Calendar.MONTH)+1;

        MonthsAndMemberCountDTO dto = new MonthsAndMemberCountDTO();
        Integer[] months = new Integer[12];
        Integer[] memberCounts = new Integer[12];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        for (int i = 0; i <12; i++) {
            //重写，获取每个月份的 第一天 和 最后一天
            String date1 = "2023-"+month+"-01";
            calendar.setTime(sdf.parse(date1));

            calendar.add(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            String firstDayOfMonth = sdf.format(calendar.getTime());

            calendar.add(Calendar.MONTH, 1);
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            String lastDayOfMonth = sdf.format(calendar.getTime());

            HashMap<Object, Object> hashMap = new HashMap<>();
            hashMap.put("startTime",firstDayOfMonth);
            hashMap.put("endTime",lastDayOfMonth);
            System.out.println("lastDayOfMonth============"+lastDayOfMonth);
            Integer memberCount = memberMapper.findMemberCountByMonths(hashMap);
            months[i] = month;
            memberCounts[i] = memberCount;
            calendar.set(Calendar.MONTH,month + 1);
            if (month == 11){
                month = month + 1;
            }else {
                month = calendar.get(Calendar.MONTH);
            }
        }
        dto.setMonths(months);
        dto.setMemberCount(memberCounts);
        return dto;
    }

    @Override
    public List<WorkCountDTO> getWorkReport()  {
        List<WorkCountDTO> list = new ArrayList<>();

        WorkCountDTO dto = new WorkCountDTO();
        dto.setName("已完成");
        Integer value = memberMapper.findWorkCountByStatus(dto);
        dto.setValue(value);
        list.add(dto);

        WorkCountDTO dto1 = new WorkCountDTO();
        dto1.setName("未到诊");
        Integer value1 = memberMapper.findWorkCountByStatus(dto1);
        dto1.setValue(value1);
        list.add(dto1);

        WorkCountDTO dto2 = new WorkCountDTO();
        dto2.setName("就诊中");
        Integer value2 = memberMapper.findWorkCountByStatus(dto2);
        dto2.setValue(value2);
        list.add(dto2);


        return list;
    }

    @Override
    public PageResult findCondition(Integer currentPage, Integer pageSize, String queryString) {
            if (currentPage==null){
                currentPage = 1;
            }
            if (pageSize==null){
                pageSize = 5;
            }
            Page<Object> page = PageHelper.startPage(currentPage,pageSize);
//        queryString="null" ,误以为有值
            if(queryString.equals("null")){
                queryString = null;
            }else {
                queryString = "%" + queryString + "%";
            }
            List<Member> checkItemList = memberMapper.findCondition(queryString);
            PageResult pageResult = new PageResult(page.getTotal(),checkItemList);
            return  pageResult;
        }

    @Override
    public void uploadFile(String fileUrl) {
        memberMapper.uploadFile(fileUrl);
    }

    @Override
    public PageResult findFileCondition(Integer currentPage, Integer pageSize, String queryString) {
        if (currentPage==null){
            currentPage = 1;
        }
        if (pageSize==null){
            pageSize = 5;
        }
        Page<Object> page = PageHelper.startPage(currentPage,pageSize);
//        queryString="null" ,误以为有值
        if(queryString.equals("null")){
            queryString = null;
        }else {
            queryString = "%" + queryString + "%";
        }
        List<File> checkItemList = memberMapper.findFileCondition(queryString);
        PageResult pageResult = new PageResult(page.getTotal(),checkItemList);
        return  pageResult;
    }

    @Override
    public void deleteFile(Integer id) {
        memberMapper.deleteFile(id);
    }

}
